package by.averianov.discounta.dataaccess.impl;

import java.util.List;

import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import javax.persistence.metamodel.SingularAttribute;

import org.hibernate.jpa.criteria.OrderImpl;
import org.springframework.stereotype.Repository;

import by.averianov.discounta.dataaccess.ShopDao;
import by.averianov.discounta.datamodel.Shop;

@Repository
public class ShopDaoImpl extends AbstractDaoImpl<Long, Shop> implements ShopDao {
	
	protected ShopDaoImpl() {
		super(Shop.class);
	}

	@Override
	public List<Shop> getAllShop(SingularAttribute<Shop, ?> attr, boolean ascending, int startRecord, int pageSize) {
		CriteriaBuilder cb = getEm().getCriteriaBuilder();

		CriteriaQuery<Shop> criteria = cb.createQuery(Shop.class);
		Root<Shop> root = criteria.from(Shop.class);

		criteria.select(root);
		criteria.orderBy(new OrderImpl(root.get(attr), ascending));
		TypedQuery<Shop> query = getEm().createQuery(criteria);
		query.setFirstResult(startRecord);
		query.setMaxResults(pageSize);

		List<Shop> results = query.getResultList();
		return results;
	}
	
	@Override
	public List<Shop> getShopsByUserId(Long userId) {
		
		/*CriteriaBuilder cb = getEm().getCriteriaBuilder();
		
		CriteriaQuery<Store> q = cb.createQuery(Store.class);
		Root<Store> root = q.from(Store.class);
		
		//root.fetch("account_2_store");
		Join<Store, UserAccount> p = root.join(Store_.toAccount);
		root.fetch(Store_.toAccount);
		q.select(root).where(cb.equal(p.get(UserAccount_.id), userId));
		//q.where(cb.equal(q.get ("account_2_store.user_account_id"), userId));
		//q.select(root);
		
		TypedQuery<Store> query = getEm().createQuery(q);
		List<Store> results = query.getResultList();
		return results;*/
		return null;
	}

}
